package com.apexwear.apexutils.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatButton;

import com.apexwear.apexutils.R;
import com.apexwear.apexutils.adapter.AdapterSelectDialog;
import com.apexwear.apexutils.api.OnPositiveClickListener;
import com.yarolegovich.discretescrollview.DSVOrientation;
import com.yarolegovich.discretescrollview.DiscreteScrollView;
import com.yarolegovich.discretescrollview.transform.ScaleTransformer;

public class SelectDialog extends Dialog {
    OnPositiveClickListener onPositiveClickListener;
    Context mContext;
    AdapterSelectDialog adapterSelectDialog;
    String[] dataList;
    String title;

    public SelectDialog(@NonNull Context context, String[] dataList, String title) {
        super(context, R.style.waiteDialog);
        this.dataList = dataList;
        this.mContext = context;
        this.title = title;
    }

    public void setOnPositiveClickListener(OnPositiveClickListener onPositiveClickListener) {
        this.onPositiveClickListener = onPositiveClickListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View dialogView = LayoutInflater.from(mContext).inflate(R.layout.dialog_select, null, false);
        TextView titleView = dialogView.findViewById(R.id.dialog_txt);
        titleView.setText(title);
        DiscreteScrollView pickerView = dialogView.findViewById(R.id.picker);
        pickerView.setOrientation(DSVOrientation.VERTICAL);
        pickerView.setItemTransitionTimeMillis(200);
        pickerView.setSlideOnFling(true);
        pickerView.setItemTransformer(new ScaleTransformer.Builder().setMinScale(0.6f).build());
        AppCompatButton sureButton = dialogView.findViewById(R.id.sure);
        sureButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (onPositiveClickListener != null) {
                    onPositiveClickListener.clickButton(dataList[pickerView.getCurrentItem()]);
                }
            }
        });
        adapterSelectDialog = new AdapterSelectDialog(dataList, mContext);
        pickerView.setAdapter(adapterSelectDialog);
        setContentView(dialogView);
        setCancelable(true);
        setCanceledOnTouchOutside(true);
    }

    public void destroyDialog() {
        mContext = null;
        onPositiveClickListener = null;
        if (adapterSelectDialog != null) {
            adapterSelectDialog.destroyAdapter();
            adapterSelectDialog = null;
        }
        dataList = null;
    }
}
